package cn.admobiletop.adsuyi.adapter.toutiao.manager;

import android.content.Context;
import android.text.TextUtils;

import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTCustomController;

import cn.admobiletop.adsuyi.ADSuyiSdk;
import cn.admobiletop.adsuyi.config.ADSuyiConfig;
import cn.admobiletop.adsuyi.config.ADSuyiInitConfig;
import cn.admobiletop.adsuyi.util.ADSuyiLogUtil;
import cn.admobiletop.adsuyi.util.ADSuyiPackageUtil;

/**
 * @author ciba
 * @description 描述
 * @date 2020/4/9
 */
public class TTAdInitManager {
    private static TTAdInitManager instance;
    private TTAdConfig ttAdConfig;
    private boolean inited;

    public static TTAdInitManager getInstance() {
        if (instance == null) {
            synchronized (TTAdInitManager.class) {
                if (instance == null) {
                    instance = new TTAdInitManager();
                }
            }
        }
        return instance;
    }

    public void init(String appId, String appKey) {
        ttAdConfig = buildConfig(appId, appKey);
        // 将初始化代码放到init中
        if (!inited && ttAdConfig != null) {
            inited = true;
            TTAdSdk.init(ADSuyiSdk.getInstance().getContext(), ttAdConfig, new TTAdSdk.InitCallback() {

                @Override
                public void success() {
                    ADSuyiLogUtil.d("toutiaoadapter init success");
                }

                @Override
                public void fail(int code, String msg) {
                    ADSuyiLogUtil.d("toutiaoadapter init fail code: " + " msg: " + msg);
                }
            });
        }
    }

    public TTAdNative createNative(Context context) {
        if (inited) {
            return TTAdSdk.getAdManager().createAdNative(context);
        }
        return null;
    }

//    public void init(String appId, String appKey) {
//        ttAdConfig = buildConfig(appId, appKey);
//    }
//
//    public TTAdNative createNative(Context context) {
//        if (!inited && ttAdConfig != null) {
//            inited = true;
//            TTAdSdk.init(ADSuyiSdk.getInstance().getContext(), ttAdConfig);
//        }
//        if (inited) {
//            return TTAdSdk.getAdManager().createAdNative(context);
//        }
//        return null;
//    }


    /**
     * 创建初始化配置
     *
     * @param appId   : 头条SDKAppId
     * @param appName : 应用名
     */
    private TTAdConfig buildConfig(String appId, String appName) {
        return new TTAdConfig.Builder()
                .appId(appId)
                // 使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景，可以使用TextureView
                .useTextureView(true)
                .appName(TextUtils.isEmpty(appName) ? ADSuyiPackageUtil.getAppName(ADSuyiSdk.getInstance().getContext()) : appName)
                .titleBarTheme(TTAdConstant.TITLE_BAR_THEME_LIGHT)
                // 是否允许sdk展示通知栏提示
                .allowShowNotify(true)
                // 是否在锁屏场景支持展示广告落地页
                .allowShowPageWhenScreenLock(true)
                // 测试阶段打开，可以通过日志排查问题，上线时去除该调用
                .debug(ADSuyiSdk.getInstance().isDebug())
                // 是否是异步初始化
//                .asyncInit(true)
                // 允许直接下载的网络状态集合
                .directDownloadNetworkType(getDirectDownloadNetworkType())
                // 是否支持多进程
                .supportMultiProcess(false)
                .customController(new TTCustomController() {
                    @Override
                    public boolean isCanUseLocation() {
                        ADSuyiInitConfig config = ADSuyiSdk.getInstance().getConfig();
                        return config == null || config.isCanUseLocation();
                    }

                    @Override
                    public boolean isCanUsePhoneState() {
                        ADSuyiInitConfig config = ADSuyiSdk.getInstance().getConfig();
                        return config == null || config.isCanUsePhoneState();
                    }

                    @Override
                    public boolean isCanUseWifiState() {
                        ADSuyiInitConfig config = ADSuyiSdk.getInstance().getConfig();
                        return config == null || config.isCanUseWifiState();
                    }

                    @Override
                    public String getDevImei() {
                        return ADSuyiSdk.getInstance().getImei(ADSuyiSdk.getInstance().getContext());
                    }

                    @Override
                    public String getDevOaid() {
                        return ADSuyiSdk.getInstance().getOAID();
                    }
                })
                .build();
    }

    /**
     * 获取可以直接下载的网络集合
     *
     * @return : 网络集合
     */
    private static int[] getDirectDownloadNetworkType() {
        int downloadTip = ADSuyiSdk.getInstance().getDownloadTip();
        switch (downloadTip) {
            case ADSuyiConfig.DownloadTip.DOWNLOAD_TIP_NOTHING:
                // 都不提示，则允许以下直接下载
                return new int[]{TTAdConstant.NETWORK_STATE_WIFI
                        , TTAdConstant.NETWORK_STATE_2G
                        , TTAdConstant.NETWORK_STATE_3G
                        , TTAdConstant.NETWORK_STATE_4G
                        , TTAdConstant.NETWORK_STATE_MOBILE};
            case ADSuyiConfig.DownloadTip.DOWNLOAD_TIP_ALL:
                // 都要提示，则没有允许直接下载的
                return new int[]{};
            default:
                return new int[]{TTAdConstant.NETWORK_STATE_WIFI};
        }
    }
}
